//
//  AppDelegate.m
//  HLAerospace
//
//  Created by hulian-ios on 17/8/29.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "AppDelegate.h"
#import "HLTabBarViewController.h"
#import "WRNavigationBar.h"
#import "HLLoginViewController.h"
#import "HLNavigationController.h"
#import "FunctionIntroManager.h"
#import "HLDownLoader.h"
#import "HLDataBaseManager.h"
#import "HLDownLoaderManager.h"
#import "HLCourseDetail.h"
#import <AFNetworking/AFNetworking.h>

@interface AppDelegate ()

@end

@implementation AppDelegate

- (NSMutableDictionary *)completionHandlerDictionary
{
    if (_completionHandlerDictionary) {
        _completionHandlerDictionary = [NSMutableDictionary dictionary];
    }
    return _completionHandlerDictionary;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    
    [self monitorNetWorking];
    
    // 创建窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    // 设置根控制器
    HLTabBarViewController *rootVc = [[HLTabBarViewController alloc] init];
    HLNavigationController *loginNav = [[HLNavigationController alloc]initWithRootViewController:[HLLoginViewController new]];
    self.window.rootViewController = [HLReadForLocation(kHLUserIsLogin) boolValue] ? rootVc: loginNav;
    // 显示窗口
    [self.window makeKeyAndVisible];
    [FunctionIntroManager showIntroPage];

    [self setNavBarAppearence];
    
    return YES;
}


- (void)monitorNetWorking
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                if ([HLReadForLocation(kHLSettingAllow4GDownloadSource) isEqualToString:@"YES"]) {
                    return ;
                }else{
                    [self pauseAll];
                }
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:{
            }
                break;
            default:
                break;
        }
    }];
}

- (void)setNavBarAppearence
{
    // 设置导航栏默认的背景颜色
    [UIColor wr_setDefaultNavBarBarTintColor:kColorNavBG];
    // 设置导航栏所有按钮的默认颜色
    [UIColor wr_setDefaultNavBarTintColor:[UIColor whiteColor]];
    // 设置导航栏标题默认颜色
    [UIColor wr_setDefaultNavBarTitleColor:[UIColor whiteColor]];
    // 统一设置状态栏样式
//    [UIColor wr_setDefaultStatusBarStyle:UIStatusBarStyleLightContent];
    // 如果需要设置导航栏底部分割线隐藏，可以在这里统一设置
    // [UIColor wr_setDefaultNavBarShadowImageHidden:YES];
}

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
    [self addCompletionHandler:completionHandler forSession:identifier];
}

- (void)addCompletionHandler:(CompletionHandlerType)handler
                  forSession:(NSString *)identifier {
    if ([self.completionHandlerDictionary objectForKey:identifier]){
        NSLog(@"Error: Got multiple handlers for a single session identifier.  This should not happen.\n");
    }
    [self.completionHandlerDictionary setObject:handler forKey:identifier];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self pauseAll];
}

- (void)pauseAll
{
    [[HLDownLoaderManager shareInstance] pauseAll];
    NSArray *detailCourses =  [HLDataBaseManager downloadingFiles:1];
    for (HLCourseDetail *detail in detailCourses) {
        detail.isPauseState = YES;
        [HLDataBaseManager updateDownloadingFile:detail];
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
